<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <canvas id="canvas" style="border: 1px solid red"></canvas>
    <script>
      const canvas = document.getElementById("canvas");
      const ctx = canvas.getContext("2d");
      // 线性渐变
      const linearGradient = ctx.createLinearGradient(0, 0, 150, 0);
      linearGradient.addColorStop(0, "red");
      linearGradient.addColorStop(0.5, "green");
      linearGradient.addColorStop(1, "blue");
      ctx.fillStyle = linearGradient;
      ctx.fillRect(0, 0, 150, 150);

      // 径向渐变
      ctx.beginPath();
      const radialGradient = ctx.createRadialGradient(225, 75, 0, 225, 35, 75);
      radialGradient.addColorStop(0, "red");
      radialGradient.addColorStop(1, "blue");
      ctx.fillStyle = radialGradient;
      ctx.arc(225, 75, 75, 0, 2 * Math.PI);
      ctx.fill();
    </script>
  </body>
</html>
